"""
This file is part of GSBEdit. For copyright and licensing information, please
see gsbedit.py in the GSBEdit base directory.
"""


import wx
import os.path

import game_data.detect_installs as di
import game_data.gsbinstall as gsbinstall
import app.gsbedit_config as gc


MS_INSTALL_SELECT = 9001
MS_MOD_SELECT = 9002
MS_SELECT = 9003
MS_EXIT = 9004
MS_PATH = 9005


class ModSelector(wx.Panel):
    def __init__(self, parent, selection_callback, has_exit=True):
        wx.Panel.__init__(self, parent)
        self.parent = parent
        self.selection_callback = selection_callback
        
        self.config = gc.GSBEditConfig('data/gsbedit.cfg')

        self.paths = di.get_valid_paths()
        self.paths.append(('', ' -- Custom location -- '))
        
        content = wx.BoxSizer(wx.VERTICAL)
        install_select_label = wx.StaticText(self, -1, 'Select the GSB install to work with:', size=(300, -1))
        self.install_select = wx.ComboBox(self, MS_INSTALL_SELECT, choices=[p[1] for p in self.paths], style=wx.CB_READONLY)
        self.install_path = wx.TextCtrl(self, MS_PATH)
        self.install_path.Enable(False)
        content.Add(install_select_label, 0, wx.EXPAND | wx.TOP | wx.LEFT | wx.RIGHT, 10)
        content.Add((1, 5), 0, wx.EXPAND)
        content.Add(self.install_select, 0, wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, 10)
        content.Add((1, 5), 0, wx.EXPAND)
        content.Add(self.install_path, 0, wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, 10)
        
        content.Add((1, 20), 0, wx.EXPAND)
        
        mod_select_label = wx.StaticText(self, -1, 'Select the active mod to work on:')
        self.mod_select = wx.ComboBox(self, MS_MOD_SELECT, choices=[], style=wx.CB_READONLY)
        content.Add(mod_select_label, 0, wx.EXPAND | wx.TOP | wx.LEFT | wx.RIGHT, 10)
        content.Add((1, 5), 0, wx.EXPAND)
        content.Add(self.mod_select, 0, wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, 10)
        
        content.Add((1, 20), 0, wx.EXPAND)
        
        buttons = wx.BoxSizer(wx.HORIZONTAL)
        self.btn_select_mod = wx.Button(self, MS_SELECT, 'Select mod')
        buttons.Add(self.btn_select_mod, 0, wx.EXPAND | wx.ALL, 10)
        buttons.Add((-1, -1), 1, wx.EXPAND)
        if has_exit:
            buttons.Add(wx.Button(self, MS_EXIT, 'Exit'), 0, wx.EXPAND | wx.ALL, 10)
        else:
            buttons.Add((0, 0), 1, wx.EXPAND | wx.ALL, 10)
        
        content.Add(buttons, 0, wx.EXPAND)

        self.SetSizer(content)
        content.Fit(parent)
        
        self.Bind(wx.EVT_TEXT, self.change_install, id=MS_INSTALL_SELECT)
        self.Bind(wx.EVT_TEXT, self.change_mod, id=MS_MOD_SELECT)
        self.Bind(wx.EVT_BUTTON, self.post_and_exit, id=MS_SELECT)
        if has_exit: self.Bind(wx.EVT_BUTTON, self.abort, id=MS_EXIT)
        self.Bind(wx.EVT_TEXT, self.path_changed, id=MS_PATH)
        
        self.install_select.SetSelection(0)
        self.change_install(None)
    
    
    def change_install(self, event):
        path, name = self.paths[self.install_select.GetSelection()]
        if path == '':
            self.custom = True
            self.install_path.Enable()
            self.install_path.SetValue(self.config.get('general', 'custom_gsb_path'))
        else:
            self.custom = False
            self.install_path.Enable(False)
            self.install_path.SetValue(path)
    
    
    def path_changed(self, event):
        path = self.install_path.GetValue()
        
        # Determine if this path is a valid GSB install by checking if it
        # exists and then attempting to detect mods
        if not(os.path.exists(path)):
            self.mod_select.SetItems([])
            self.mod_select.SetValue('')
            self.btn_select_mod.Enable(False)
            print "Directory does not exist (or we don't have access permissions)"
            return
            
        self.gsbi = gsbinstall.GSBInstall(path)
        #try:
        if not(self.gsbi.find_mods()):
            print "Mod detection failed (return)"
            return
        #except:
        #    print "Mod detection failed (exception)"
        #    return
        
        # Get a list of mods to display
        self.mod_list = self.gsbi.get_mod_list_friendly()
        self.mod_select.SetItems(self.mod_list)
        self.mod_select.SetSelection(0)
        self.change_mod(None)
        self.btn_select_mod.Enable()
        
        # If we got this far then the path is valid, so store it
        if self.custom: self.config.set('general', 'custom_gsb_path', path)
    
    
    def change_mod(self, event):
        current_mod = self.mod_list[self.mod_select.GetSelection()]
        self.gsbi.set_current_mod_by_friendly_name(current_mod)
        
    
    def post_and_exit(self, event):
        self.post_selection()
        self.parent.close(None)
    
    
    def post_selection(self):
        self.selection_callback(self.gsbi)
    
    
    def abort(self, event):
        wx.Exit()


class ModSelectorWindow(wx.Frame):
    def __init__(self, parent, selection_callback, modal, has_exit=True):
        wx.Frame.__init__(self, parent, -1, 'Active mod selection', style=wx.CAPTION)
        
        self.modal = modal
        
        if self.modal: self.MakeModal()
        
        self.parent = parent
        
        self.Bind(wx.EVT_CLOSE, self.close)
        
        self.panel = ModSelector(self, selection_callback, has_exit=has_exit)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel, 1, wx.EXPAND)

        self.SetSizer(sizer)
        
        self.Center()
        self.Show(True)
        self.Raise()
    
    
    def close(self, event):
        if self.modal: self.MakeModal(False)
        #self.panel.post_selection()
        self.Destroy()
        
